import request from "@/utils/request";

// 运单综合查询接口
export function getShipmentComprehensive(query) {
  return request({
    url: "/system/bill/list",
    method: "get",
    params: query,
  });
}

// 运单详情查询接口
export function getShipmentDetail(id) {
  return request({
    url: "/system/bill/" + id,
    method: "get",
  });
}

// 获取运单定位日志列表（不分页）
export function getLocationLogList(waybillId) {
  return request({
    url: "/pack/locationLog/list",
    method: "get",
    params: {
      containerId: waybillId, // 运单ID作为containerId查询
      pageSize: 9999, // 设置一个很大的数来获取全部数据
      pageNum: 1,
    },
  });
}

// 运单轨迹查询接口
export function getShipmentTracking(shipmentId, params = {}) {
  return request({
    url: "/system/bill/track",
    method: "get",
    params: {
      shipmentId,
      ...params,
    },
  })
    .then((response) => {
      // 处理后端返回的数据，应用mock数据的处理逻辑
      if (response && response.code === 200) {
        const { shipmentInfo, trackingList } = response.data || response;

        if (params.viewMode === "daily") {
          // 按天分组轨迹数据
          const dailyTracking = {};
          const tracks = trackingList || [];

          tracks.forEach((track) => {
            // 使用trackTime字段而不是timestamp
            const date = track.trackTime ? track.trackTime.split(" ")[0] : "";
            if (date && !dailyTracking[date]) {
              dailyTracking[date] = [];
            }
            if (date) {
              dailyTracking[date].push(track);
            }
          });

          // 将对象转换为数组，并按日期排序
          const dailyTrackingArray = Object.keys(dailyTracking)
            .sort((a, b) => new Date(b) - new Date(a)) // 按日期倒序排列
            .map((date) => ({
              date,
              tracks: dailyTracking[date],
            }));

          return {
            code: 200,
            msg: "success",
            data: {
              shipmentInfo,
              dailyTracking: dailyTrackingArray,
            },
          };
        } else {
          // 总体轨迹，直接返回后端数据
          return {
            code: 200,
            msg: "success",
            data: {
              shipmentInfo,
              trackingList: trackingList || [],
            },
          };
        }
      } else {
        // 如果后端返回失败，返回空数据
        return {
          code: (response && response.code) || 500,
          msg: (response && response.msg) || "查询失败",
          data: {
            shipmentInfo: null,
            trackingList: [],
            dailyTracking: [],
          },
        };
      }
    })
    .catch((error) => {
      console.error("查询运单轨迹失败:", error);
      // 网络错误或其他异常，返回标准格式的空数据
      return {
        code: 500,
        msg: "网络异常，查询失败",
        data: {
          shipmentInfo: null,
          trackingList: [],
          dailyTracking: [],
        },
      };
    });
}
// 导出运单数据
export function exportShipment(query) {
  return request({
    url: "/system/bill/export",
    method: "post",
    data: query,
    responseType: "blob",
  });
}

// 运单状态选项
export const shipmentStatusOptions = [
  { label: "全部", value: "" },
  { label: "待发货", value: "1" },
  { label: "运输中", value: "2" },
  { label: "已签收", value: "3" },
  { label: "已取消", value: "4" },
];

// 延时状态选项
export const delayStatusOptions = [
  { label: "正常", value: "正常", type: "success" },
  { label: "轻微延时", value: "轻微延时", type: "warning" },
  { label: "严重延时", value: "严重延时", type: "danger" },
];

// 货物状况选项
export const goodsConditionOptions = [
  { label: "完好", value: "完好", type: "success" },
  { label: "轻微损坏", value: "轻微损坏", type: "warning" },
  { label: "严重损坏", value: "严重损坏", type: "danger" },
];

// 交接情况选项
export const handoverStatusOptions = [
  { label: "已交接", value: "已交接", type: "success" },
  { label: "待交接", value: "待交接", type: "warning" },
  { label: "交接异常", value: "交接异常", type: "danger" },
];
